<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
use Common\Model\Category;
use Common\Model\Shop;
use Common\Model\Shop_attribute;
use Common\Model\Attribute;
class ListController extends Controller{
	public function index(){
		//3.根据分类cid或品牌id获得商品的shop_id
		$cid=Q('get.cid',0,'intval');
		$bid = Q('get.id',0,'intval');
		$ShopModel=new Shop;
		$CateModel=new Category;
		if($bid){
			$shop_ids = $ShopModel->where("brand_id= {$bid}")->lists('sid');
		}elseif($cid){
			//2.通过当前分类的cid获得所有子集的cid
			$cids=$CateModel->getSon($CateModel->get(),$cid);
			$cids[]=$cid;
			$ShopModel=new Shop;
			//p($brandCids);exit;
			$shop_ids=$ShopModel->where("category_cid IN(".implode(',', $cids).")")->lists('sid');
		}
		if($shop_ids){
			$Shop_attrModel=new Shop_attribute;
			$shop_attr=$Shop_attrModel->where("shop_id IN(".implode(',', $shop_ids).")")->groupBy('shop_attributecol')->get();
	//		把相同attribute_aid的属性放在了一起
			$sameTemp=[];
			foreach($shop_attr as $v){
				$sameTemp[$v['attribute_aid']][]=$v;
			}
			//组合名字和值
			$finalAttr=[];
			$AttributeModel=new Attribute;
			foreach($sameTemp as $attribute_aid=>$v){			
					$finalAttr[]=[
					'name'=>$AttributeModel->where("aid={$attribute_aid}")->pluck('aname'),
					'value'=>$v
				];			
			}
			View::with('finalAttr',$finalAttr);	
//			p($finalAttr);
			 //筛选效果********************
            //1.地址栏效果0_0_...
            $num = count($finalAttr);
//          p($num);
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) : array_fill(0,$num,0);
//          p($param);
            View::with("param",$param);
            //2.筛选操作,最终获得商品gid      
            $finalGids = $this->filter($param,$shop_ids);
//          p($finalGids);exit;
			$shopModel=new Shop;
			if($finalGids){
				$data=$shopModel->where("sid IN(".implode(',', $finalGids).")")->get();			
				View::with('data',$data);
				$counts=count($data);
			}else{
				View::with('data',[]);
				$counts=0;
			}
		}else{
			$counts=0;
			View::with('data',[]);
		 	View::with('finalAttr',[]);		
		}
		View::with('counts',$counts);
		View::make();
	}
	private function filter($param,$cidGids){
        $shopAttrModel = new Shop_attribute();
        $sidArr = [];
        foreach ($param as $shop_id) {
            //如果点击的不是"不限"的时候
            //根据gtid得到商品的gid
            if($shop_id){
                //查询到商品属性的名称
                $gtvalue = $shopAttrModel->where("sid={$shop_id}")->pluck('shop_attributecol');
                //通过名称查询到gid
                $sidArr[] = $shopAttrModel->where(['shop_attributecol'=>$gtvalue])->lists('shop_id');
            }
        }
//        p($sidArr);
        //如果$sidArr有值的时候,也就是用户不是全部"不限"的时候       
        if($sidArr){
            $sid = $sidArr[0];			
            foreach ($sidArr as $v) {
                $sid = array_intersect($v,$sid);
            }
            //因为$sid有可能是其他分类的sid,那么为了确保是当前分类,再次取交集
            $finalGids = array_intersect($sid,$cidGids);
        }else{//如果全部是"不限"的时候
            $finalGids = $cidGids;	
        }
        return $finalGids;       
    }

    //搜索
    public function ss(){
    	$ss=Q('post.ss');
//		p($ss);
    	$Shop=new Shop;
    	$data=$Shop->where("sname like '%".$ss."%'")->get();
//    	p($data);exit;
    	$counts=count($data);
    	View::with('counts',$counts);
    	View::with('data',$data);
		View::make();
    }
}
	
	
	
	
	